﻿$(function () {
    //时间预约 Order
    var TimeNum1YStart, TimeNum2YStart,
        TimeNum1YEnd, TimeNum2YEnd,
        i = 0, j = 0,
        TimeLR,
        TimeNum1YPos = 0, TimeNum2YPos = 0;
    document.addEventListener("touchstart", touchStart, false);
    document.addEventListener("touchmove", touchMove, false);
    document.addEventListener("touchend", touchEnd, false);

    function touchStart(e) {
        if (e.touches[0].clientX < 160) {
            TimeNum1YStart = e.touches[0].clientY;
            TimeLR = 1;
        } else {
            TimeNum2YStart = e.touches[0].clientY;
            TimeLR = 2;
        }
    }
    function touchMove(e) {
        e.preventDefault();
        if (TimeLR == 1) {
            $('.OrderTimeFir').css('top', parseInt(e.changedTouches[0].clientY - TimeNum1YStart) + parseInt(TimeNum1YPos) + 'px');
            i = Math.abs((parseInt($('.OrderTimeFir').css('top')) - 13) / 27).toFixed(0);
            $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeFir li').eq(i).addClass('OrderTimeFirFocus');
        } else if (TimeLR == 2) {
            $('.OrderTimeSec').css('top', parseInt(e.changedTouches[0].clientY - TimeNum2YStart) + parseInt(TimeNum2YPos) + 'px');
            //console.log($('.OrderTimeSec').css('top'));
            j = Math.abs((parseInt($('.OrderTimeSec').css('top')) - 27) / 27).toFixed(0);
            $('.OrderTimeSec li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeSec li').eq(j).addClass('OrderTimeFirFocus');
        }
    }
    var touchHeight = 27, EndI, EndJ;
    todayUl();
    function touchEnd(e) {
        if (TimeLR == 1) {
            EndI = -(parseInt($('.OrderTimeFir').css('top')) - 13) / touchHeight;
            EndI = EndI.toFixed(0)
            $('.OrderTimeFir').css('top', -touchHeight * (EndI - 1) + 'px');
            $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeFir li').eq(EndI).addClass('OrderTimeFirFocus');
            if ($('.OrderTimeFir li').eq(EndI).text() == "今天") {
                todayUl();
            }
            else {
                allDayUl();
            }
            if (parseInt($('.OrderTimeFir').css('top')) >= 0) {
                $('.OrderTimeFir').css('top', '0px');
                $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeFir li').eq(1).addClass('OrderTimeFirFocus');
                todayUl();
            }
            if (parseInt($('.OrderTimeFir').css('top')) <= -(touchHeight * $('.OrderTimeFir li').length - 1) + 13) {
                $('.OrderTimeFir').css('top', '-54px');
                $('.OrderTimeFir li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeFir li').eq(3).addClass('OrderTimeFirFocus');
                console.log("小于0：" + $('.OrderTimeFir li').eq(3).text());
                allDayUl();
            }
        }
        if (TimeLR == 2) {
            EndJ = -(parseInt($('.OrderTimeSec').css('top')) - 32) / touchHeight;
            EndJ = EndJ.toFixed(0)
            $('.OrderTimeSec').css('top', -touchHeight * (EndJ - 1) + 'px');
            $('.OrderTimeSec li').removeClass('OrderTimeFirFocus');
            $('.OrderTimeSec li').eq(EndJ).addClass('OrderTimeFirFocus');
            if (parseInt($('.OrderTimeSec').css('top')) >= 32) {
                $('.OrderTimeSec').css('top', '0');
                $('.OrderTimeSec li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeSec li').eq(1).addClass('OrderTimeFirFocus');
            }
            if (parseInt($('.OrderTimeSec').css('top')) <= -(touchHeight * $('.OrderTimeSec li').length - 2) + 5) {
                $('.OrderTimeSec').css('top', -(touchHeight * $('.OrderTimeSec li').length - 2));
                $('.OrderTimeSec li').removeClass('OrderTimeFirFocus');
                $('.OrderTimeSec li').eq($('.OrderTimeSec li').length - 1).addClass('OrderTimeFirFocus');
            }
        }
        TimeNum1YPos = $('.OrderTimeFir').css('top');
        TimeNum2YPos = $('.OrderTimeSec').css('top');
    }
    $('.OrderTimeBtn a').eq(1).click(function () {
        $('.OrderGray').hide();
        $('.OrderTimeChoose').hide();
        $('body').css('overflow', 'scroll');
        $('.OrderTime span').text($('.OrderTimeFir').children('.OrderTimeFirFocus').text() + " " + $('.OrderTimeSec').children('.OrderTimeFirFocus').text());
        //测试时间转换
        test();
    });
    $('.OrderTimeBtn a').eq(0).click(function () {
        $('.OrderGray').hide();
        $('.OrderTimeChoose').hide();
        $('body').css('overflow', 'scroll');
    });
    $('.OrderTime a').click(function () {
        $('.OrderGray').show();
        $('.OrderTimeChoose').show();
        $('body').css('overflow', 'hidden');
    })
    $('.OrderGray').click(function () {
        $('.OrderGray').hide();
        $('.OrderTimeChoose').hide();
        $('body').css('overflow', 'scroll');
    });
    $('.OrderTimeFir').children('.OrderTimeFirFocus')
});
//当天时间逻辑
function todayUl() {
    var currentDate = new Date();
    var curHour = Number(currentDate.getHours());
    var str = "";
    str += "<li>一小时送达</li>";
    for (var i = curHour; i < 24; i++) {
        if (i == curHour) {
            str += "<li class=\"OrderTimeFirFocus\">" + i + ":00-" + (i + 1) + ":00</li>";
        }
        else {
            str += "<li>" + i + ":00-" + (i + 1) + ":00</li>";
        }
    }
    var strLi = str.toString();
    $('.OrderTimeSec').html(strLi);
}
//明后天时间逻辑
function allDayUl() {
    var str = "";
    for (var i = 0; i < 24; i++) {
        if (i == 0) {
            str += "<li>一小时送达</li>";
        }
        else if (i == 1) {
            str += "<li class=\"OrderTimeFirFocus\">0:00-1:00</li>";
        }
        else {
            str += "<li>" + i + ":00-" + (i + 1) + ":00</li>";
        }
    }
    var strLi = str.toString();
    $('.OrderTimeSec').html(strLi);
}